import { CreepAbilityHelper } from "utils/creep/CreepAbilityHelper";
import { ActionBase, ActionBaseOfCreep } from "../ActionBase";
import { EActionCode } from "framework/FSM/Action/ActionCode";

/**
 * 蓝绿绿一体机 攻击行为
 */
export class ActionIADAttackBlue  extends ActionBaseOfCreep<IActionCMD<"attackBlue">>
{
    private isFlee(c:Creep, em:Creep)
    {
       
        if( em.getType() == "Creep"&& em.getActiveBodyparts(ATTACK) && em.pos.distance(c.pos)<=2)
        {
            c.say(`👋${em.pos.distance(c.pos)}`)
            return true;
        }
        const cache = c.room.getWorld().PollHostiles(c.pos.roomName);
        if(cache )
        {
            if((cache.inquirePossibleDamage(c.nextPosition())>c.getAbilityHeal() && c.hits < c.hitsMax))
            {
                // 对面伤害大于我的治疗
                c.say("🏃")
                return true
            }
            if(em.hits>CreepAbilityHelper.getCreepRangeAttackAbility(c)&& c.hits < c.hitsMax)
            {
                c.say("✋")
                // 不能最后一击
                return true;
            }
        }
        c.say("👊")   
        return false;
    }
    protected updateArgs(action: IActionCMD<"attackBlue">): EActionCode 
    {
        if(super.updateArgs(action) !== EActionCode.OK)
        {
            return EActionCode.FAILED;
        }
        // const tactic = action.arg.entity.getTactic();
        // if(!tactic.targets || tactic.targets.length ==0)
        // {
        //     return EActionCode.FAILED;
        // }
         
        action.arg.enemy = Game.getObjectById( action.arg.enemy.id)! as AttackObject
        if(!action.arg.enemy)
        {
            return EActionCode.FAILED;
        }
        return EActionCode.OK;
    }
    protected _execute(action: IActionCMD<"attackBlue">): ActionCode 
    {
         
        const creep = action.arg.entity;
        const around = creep.getAroundHostileCreeps();
     
        if(creep.pos.roomName == action.arg.enemy.pos.roomName)
        {
            const enemy = action.arg.enemy  as Creep;
            if(this.isFlee(creep,enemy))
            {
                 
                creep.flee([{pos:enemy.pos,range:4}],{}) 
            }
            else 
            {
                 
                creep.mymoveTo({pos:action.arg.enemy.pos,range:0},{});
            }
            if(creep.pos.distance(action.arg.enemy.pos)>3)
            {
                if(around.some(e=>e.pos.inRangeTo(creep,1)))
                {
                    creep.rangedMassAttack( );
                }
                else
                {
                    creep.rangedAttack( _.max(around,e=>e.getActiveBodyparts(HEAL)));
                }
            }
            else 
            {
                if(around.concat([action.arg.enemy as Creep]).some(e=>e.pos.inRangeTo(creep,1)))
                {
                    creep.rangedMassAttack( );
                }
                else 
                {
                    creep.rangedAttack(action.arg.enemy);
                }
            }
        }
        else 
        {
            
            if(around.length)
            {
                const enemy = _.min(around,e=>e.pos.distance(creep.pos));
                /**
                 * 逃跑条件.
                 * 地方距离我只有2格
                 * 我没有攻击能力了
                 * 
                */
                if(this.isFlee(creep,enemy))
                {
                    
                    creep.flee([{pos:enemy.pos,range:4}],{}) 
                }
                else 
                {
                   
                    creep.mymoveTo({pos:action.arg.enemy.pos,range:0},{});
                }
                if(around.concat([action.arg.enemy as Creep]).some(e=>e.pos.inRangeTo(creep,1)))
                {
                    creep.rangedMassAttack( );
                }
                else 
                {
                
                    creep.rangedAttack(enemy);
                }
            }
            else 
            {
                creep.mymoveTo({pos:action.arg.enemy.pos,range:0},{});
            }
        }
        
        if(creep.memory.body[HEAL] && (creep.hits<creep.hitsMax || action.arg.enemy.pos.roomName == creep.pos.roomName))
        {
            creep.heal(creep);
        }
        else 
        {
            creep.healAroundCreep()
        }
        
        return  EActionCode.OK;
    }
}